package com.juzipi.demo.realm;


import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * 使用自定义realm
 */
@Slf4j
public class TestCustomerRealmAuthenticator {
    public static void main(String[] args) {

        //创建securityManager
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        //设置自定义realm
        CustomerRealm customerRealm = new CustomerRealm();
        securityManager.setRealm(customerRealm);
        //设置安全工具类
        SecurityUtils.setSecurityManager(securityManager);
        //通过安全工具类获取subject
        Subject subject = SecurityUtils.getSubject();
        //创建令牌
        UsernamePasswordToken token = new UsernamePasswordToken("guest","guest");
        try {
            subject.login(token);
            log.info(String.valueOf(subject.isAuthenticated()));
            log.info("登录SUCCESS");
        }catch (IncorrectCredentialsException e){
            log.info("=========>>>>>  密码错误");
            e.printStackTrace();
        } catch (AuthenticationException e) {
            log.info("=========>>>>>  用户名错误");
            e.printStackTrace();
        }


    }

}
